package net.javastone.toolkit.robot.dingding.advice;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Created by sdliang on 2018-10-26.
 */
@RestControllerAdvice
public class ExceptionAdvice {

  private Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class);

  @ExceptionHandler(value = {RuntimeException.class})
  public ResponseEntity runtimeException(RuntimeException e) {
    logger.error(e.getMessage(), e);
    return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
        .body(String.format("服务异常: %s", e.getMessage()));
  }

  @ExceptionHandler(value = {Exception.class})
  public ResponseEntity exception(Exception e) {
    logger.error(e.getMessage(), e);
    return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
        .body(String.format("服务异常: %s", e.getMessage()));
  }

}
